package org.iartisan.admin.template.controller.support.rest;

import org.iartisan.admin.template.service.support.JnlsTraceSeqSupport;
import org.iartisan.runtime.utils.UUIDUtil;
import org.iartisan.runtime.web.WebR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 序列支持controller
 *
 * @author King
 * @date 2020-02-03
 */
@RestController
@RequestMapping("seqSupport")
public class SeqSupportRestController {

    @Autowired
    private JnlsTraceSeqSupport jnlsTraceSeqSupport;

    @GetMapping("nextval")
    public WebR nextval(String seqName) {
        WebR r = new WebR();
        int nextval = jnlsTraceSeqSupport.nextval(seqName);
        r.setData(nextval);
        return r;
    }

    @GetMapping("currval")
    public WebR currval(String seqName) {
        WebR r = new WebR();
        int currval = jnlsTraceSeqSupport.currval(seqName);
        r.setData(currval);
        return r;
    }

    @GetMapping("setval")
    public WebR setval(String seqName, int value) {
        WebR r = new WebR();
        int setval = jnlsTraceSeqSupport.setval(seqName, value);
        r.setData(setval);
        return r;
    }

    @GetMapping("genOrderId")
    public WebR genOrderId(String seqName) {
        WebR r = new WebR();
        //生成订单号
        int nextval = jnlsTraceSeqSupport.nextval(seqName);
        String orderId = "T" + UUIDUtil.timeBaseId(12) + String.format("%06d", nextval);
        r.setData(orderId);
        return r;
    }
}
